package cn.iocoder.yudao.module.system.service.auth;

import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthWechatLoginStatusRespVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;

/**
 * 微信登录状态管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Slf4j
public class WechatLoginStatusServiceImpl implements WechatLoginStatusService {

    private static final String WECHAT_LOGIN_STATE_KEY_PREFIX = "wechat_login_state:";
    private static final Duration EXPIRE_TIME = Duration.ofMinutes(5); // 5分钟过期

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void savePendingStatus(String state) {
        AuthWechatLoginStatusRespVO status = AuthWechatLoginStatusRespVO.builder()
                .status("pending")
                .build();
        saveToRedis(state, status);
        log.info("[savePendingStatus][state({}) 保存等待状态]", state);
    }

    @Override
    public void saveSuccessStatus(String state, String accessToken, String refreshToken, 
                                 Long userId, String nickname, String avatar) {
        AuthWechatLoginStatusRespVO status = AuthWechatLoginStatusRespVO.builder()
                .status("success")
                .accessToken(accessToken)
                .refreshToken(refreshToken)
                .userId(userId)
                .nickname(nickname)
                .avatar(avatar)
                .build();
        saveToRedis(state, status);
        log.info("[saveSuccessStatus][state({}) 保存成功状态, userId({})]", state, userId);
    }

    @Override
    public void saveFailedStatus(String state, String errorMsg) {
        AuthWechatLoginStatusRespVO status = AuthWechatLoginStatusRespVO.builder()
                .status("failed")
                .errorMsg(errorMsg)
                .build();
        saveToRedis(state, status);
        log.info("[saveFailedStatus][state({}) 保存失败状态, errorMsg({})]", state, errorMsg);
    }

    @Override
    public AuthWechatLoginStatusRespVO getLoginStatus(String state) {
        String key = WECHAT_LOGIN_STATE_KEY_PREFIX + state;
        String value = stringRedisTemplate.opsForValue().get(key);
        if (value == null) {
            return null;
        }
        return JsonUtils.parseObject(value, AuthWechatLoginStatusRespVO.class);
    }

    @Override
    public void deleteLoginStatus(String state) {
        String key = WECHAT_LOGIN_STATE_KEY_PREFIX + state;
        stringRedisTemplate.delete(key);
        log.info("[deleteLoginStatus][state({}) 删除登录状态]", state);
    }

    /**
     * 保存到Redis
     */
    private void saveToRedis(String state, AuthWechatLoginStatusRespVO status) {
        String key = WECHAT_LOGIN_STATE_KEY_PREFIX + state;
        String value = JsonUtils.toJsonString(status);
        stringRedisTemplate.opsForValue().set(key, value, EXPIRE_TIME);
    }

}